package BitCreeekClient;


import BitCreekLib.MetaInfo;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.rmi.ConnectException;
import java.util.ArrayList;
import java.util.Map;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;

public class applicationWindow extends JDialog {
    
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
    private JTabbedPane tabs;

    private JList logsJList;
    private JList searchResultsJList;

    private JTextField searchJTextField;
    private JTextField saveLocationJTextField;
    private JTextField localPathJTextField;
    private JTextField torrentIPJTextField;
    private JTextField downloadLocationJTextField;
    private JTextField creekLocationJTextField;
    private JTextField torrentLocationJTextField;

    private JButton searchButton;
    private JButton downloadButton;
    private JButton publishButton;
    private JButton buttonOK;
    private JButton okButton;
    private JButton browseButton;

    private JTable tableUploads;
    private JFileChooser fc;

    private Client client;
    ArrayList<MetaInfo> matches;

    static protected GUIHandler c;

    // Models:
    DefaultListModel LOGmodel;
    DefaultTableModel uploadTableModel;
    DefaultListModel searchModel;

    protected MetaInfo result;

    public applicationWindow () {
        super ( null, ModalityType.TOOLKIT_MODAL );
        setContentPane ( contentPane );

        getRootPane ().setDefaultButton ( buttonOK );
        setTitle ( "BitCreeK" );
        BufferedImage image = null;
        try {
            image = ImageIO.read ( getClass ().getResource ( "res/monitor.png" ) );
        } catch ( IOException e ) {
            e.printStackTrace ();
        }
        setIconImage ( image );

        fc = new JFileChooser ();
        fc.setFileSelectionMode ( JFileChooser.FILES_ONLY );

        // Log list
        LOGmodel = new DefaultListModel ();
        Log.logOutput = LOGmodel;
        logsJList.setModel ( LOGmodel );

        searchModel = new DefaultListModel ();
        searchResultsJList.setModel ( searchModel );

        // Download List
        uploadTableModel = new DefaultTableModel ();
        tableUploads.setModel ( uploadTableModel );
        tableUploads.setEnabled ( false );
        uploadTableModel.addColumn ( "File Name" );
        uploadTableModel.addColumn ( "Size" );
        uploadTableModel.addColumn ( "Parts" );
        uploadTableModel.addColumn ( "Tracker" );
        uploadTableModel.addColumn ( "Progress" );
        uploadTableModel.addColumn ( "Peers" );
        uploadTableModel.addColumn ( "Location" );
        uploadTableModel.addColumn ( "Up" );
        uploadTableModel.addColumn ( "DL" );

        TableColumn column = tableUploads.getColumnModel ().getColumn ( 4 );
        column.setCellRenderer ( new ProgressRenderer () );

        tableUploads.setRowSelectionAllowed ( true );
        /* Izilizing form listeners */
        Log.add ( "Inizializing Listners " );


        searchResultsJList.addListSelectionListener ( new ListSelectionListener() {
            public void valueChanged ( ListSelectionEvent listSelectionEvent ) {
                if ( searchResultsJList.getSelectedIndex () == -1 ) return;
                result = matches.get ( searchResultsJList.getSelectedIndex () );
                saveLocationJTextField.setText ( downloadLocationJTextField.getText () + result.getName () );
            }
        } );
        
        okButton.addActionListener ( new ActionListener() {
            public void actionPerformed ( ActionEvent e ) {
                onSaveConfig();
            }
        } );
        
        publishButton.addActionListener ( new ActionListener() {
            public void actionPerformed ( ActionEvent e ) {
                onPublish ();
            }
        } );

        browseButton.addActionListener ( new ActionListener() {
            public void actionPerformed ( ActionEvent e ) {
                int returnVal = fc.showOpenDialog ( applicationWindow.this );
                if ( returnVal == JFileChooser.APPROVE_OPTION ) {
                    File file = fc.getSelectedFile ();
                    localPathJTextField.setText ( file.getAbsolutePath () );
                }
            }
        } );
        searchButton.addActionListener ( new ActionListener() {
            public void actionPerformed ( ActionEvent e ) {
                onSearch ();
            }
        } );
        downloadButton.addActionListener ( new ActionListener() {
            public void actionPerformed ( ActionEvent e ) {
                onDownload ();
            }
        } );
        /* CLIENT PART INIT */
        Log.add ( "Inizializing Client " );
        client = new Client ();
        client.intit ();

        c = new GUIHandler ();
        c.addGUIEventListener ( new GUIEventListener() {
            public void guiEventOccurred ( GUIEvent evt ) {
                Download source = ( Download ) evt.getSource ();
                boolean updated = false;
                for ( int i = 0; i < uploadTableModel.getRowCount () && !updated; i++ ) {
                    if ( uploadTableModel.getValueAt ( i, 0 ).equals ( source.getName () ) ) {
                        uploadTableModel.setValueAt ( source.getName (), i, 0 );
                        uploadTableModel.setValueAt ( Utils.humanSize ( source.metaInfo.getSize () ), i, 1 );
                        uploadTableModel.setValueAt ( String.valueOf ( source.getPossesedParts () ) + " / " + String.valueOf ( source.metaInfo.getNumParts () ), i, 2 );
                        uploadTableModel.setValueAt ( source.trackerIP, i, 3 );
                        uploadTableModel.setValueAt ( String.valueOf ( source.getProgress () ), i, 4 );
                        uploadTableModel.setValueAt ( String.valueOf ( source.connectedPeers.size () ) + " / " + String.valueOf ( source.swarmInfo.size () ), i, 5 );
                        uploadTableModel.setValueAt ( source.localPath, i, 6 );
                        uploadTableModel.setValueAt ( String.valueOf ( source.getUploadBand () ), i, 7 );
                        uploadTableModel.setValueAt ( String.valueOf ( source.getDownloadBand () ), i, 8 );
                        updated = true;
                    }
                }
                if ( !updated ) {
                    uploadTableModel.addRow ( source.vectorize () );
                }

            }
        } );
        c.addGUIErrorListener ( new GUIErrorListener() {
            public void guiErrorOccurred ( GUIError err ) {
                
            	
            	//use the error and show it to the User
            	Log.add ( ((Exception) err.getSource()).toString() );
                JOptionPane.showMessageDialog ( contentPane.getParent(), ((Exception) err.getSource()).toString(), ((Exception) err.getSource()).getCause().toString(), JOptionPane.ERROR_MESSAGE );
            	
            }
        } );
        

        
        addWindowListener ( new WindowAdapter() {
            public void windowClosing ( WindowEvent evt ) {
                onClose ();
                
                System.exit ( 0 );
            }
        } );

        onLoad ();
    }
    
    
    public static void main ( String[] args ) {
    	
    	try {
			PeerConfig.initConfig();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
    	
        JDialog.setDefaultLookAndFeelDecorated ( true );
        try {
            UIManager.setLookAndFeel ( new SubstanceBusinessBlackSteelLookAndFeel () );
        } catch ( Exception e ) {
            System.out.println ( "Substance BusinessBlackSteel failed to initialize" );
        }
        SwingUtilities.invokeLater ( new Runnable() {
            public void run () {
                applicationWindow dialog = new applicationWindow ();
                dialog.pack ();
                dialog.setVisible ( true );
            }
        } );
    }
    
    private void onSaveConfig() {
    	PeerConfig.setTorrentsDirPath(torrentLocationJTextField.getText());
    	PeerConfig.setDownloadsDiPath(downloadLocationJTextField.getText());
    	PeerConfig.setServerAddress(torrentIPJTextField.getText());

	//saving the config
    	try {
		PeerConfig.saveConfig();
		Log.add ( "Config saved!" );
	} catch (IOException e) {
                JOptionPane.showMessageDialog ( this, "Impossible to save the config file during close.", "Config save.", JOptionPane.ERROR_MESSAGE );    
	}
    	
    }

    private void onPublish () {
        if ( localPathJTextField.getText ().isEmpty () ) {
            JOptionPane.showMessageDialog ( this, "Please select file first.", "Select File ", JOptionPane.OK_OPTION );
            return;
        }
        Log.add ( "Torrent tracker IP: " + torrentIPJTextField.getText () );
        Client.TrackerIP = torrentIPJTextField.getText ();
        try {
            client.publishTorrent ( localPathJTextField.getText () );
            tabs.setSelectedIndex ( 0 );
        } catch ( ConnectException e ) {
        	//applicationWindow.c.fireGUIError ( new GUIError ( e ) );
            Log.add ( "Connection Error!: " + e.toString () );
            JOptionPane.showMessageDialog ( this, e.getCause (), "Connection Error! ", JOptionPane.ERROR_MESSAGE );
        } catch ( Exception e ) {
            Log.add ( "Error: publishTorrent: " + e.toString () );
            JOptionPane.showMessageDialog ( this, e.toString (), "Publish error!", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace ();
        }
    }

    private void onSearch () {
        Client.TrackerIP = torrentIPJTextField.getText ();

        try {
            matches = client.searchTorrentByName ( searchJTextField.getText () );
            searchModel.removeAllElements ();
            for ( MetaInfo match : matches ) {
                searchModel.addElement ( match.toString () );
                Log.add ( match.toString () );
            }
            if ( !searchModel.isEmpty () ) searchResultsJList.setSelectedIndex ( 0 );
        } catch ( ConnectException e ) {
            Log.add ( "Connection Error!: " + e.toString () );
            JOptionPane.showMessageDialog ( this, e.getCause (), "Connection Error! ", JOptionPane.ERROR_MESSAGE );
        	//applicationWindow.c.fireGUIError ( new GUIError ( e ) );
        } catch ( Exception e ) {
            Log.add ( "Error: searchTorrentByName: " + e.toString () );
            JOptionPane.showMessageDialog ( this, e.toString (), "Search error!", JOptionPane.ERROR_MESSAGE );
        }
    }

    private void onDownload () {
        result = null;
        if ( searchResultsJList.getSelectedIndex () != -1 ) {
            result = matches.get ( searchResultsJList.getSelectedIndex () );
            String path = saveLocationJTextField.getText ();
            if ( result != null ) {
                if ( !client.checkExistingDownload ( result ) ) {
                    Log.add ( "Downloading" + result.toString () + " To: " + path );
                    try {
                        client.startDownload ( result, path );
                    } catch ( Exception e ) {
                        Log.add ( "Error: startDownload: " + e.toString () );
                        JOptionPane.showMessageDialog ( this, e.toString (), "startDownload error!", JOptionPane.ERROR_MESSAGE );
                        e.printStackTrace ();
                    }
                    tabs.setSelectedIndex ( 0 );
                } else {
                    Log.add ( "Torrent" + result.toString () + " already downloaded or published." );
                    JOptionPane.showMessageDialog ( this, "Torrent" + result.toString () + " already downloaded or published.", "startDownload notification!", JOptionPane.INFORMATION_MESSAGE );
                }
            }
        }
    }

    private void onClose () {
    	
    	//saving the config
    	try {
			PeerConfig.saveConfig();
			Log.add ( "Config saved!" );
		} catch (IOException e) {
                JOptionPane.showMessageDialog ( this, "Impossible to save the config file during close.", "Config save.", JOptionPane.ERROR_MESSAGE );
            
		}
    	
        File folder = PeerConfig.getTorrentsDir();
        if ( !folder.exists () ) {
            folder.mkdirs ();
        }
        for ( Map.Entry<String, Download> sd : Client.downloads.entrySet () )
            sd.getValue ().saveMetaInfo ( PeerConfig.getTorrentsDirPath() + sd.getValue ().getName () + ".bc" );
        Log.add ( "Torents informations saved!" );
    }

    private void onLoad () {
    	
    	Client.TrackerIP = torrentIPJTextField.getText ();
    	
        // Se ci sono dei torrent nella cartella li carico tutti
        File dir = PeerConfig.getTorrentsDir();
        if ( !dir.exists () ) {
            return;
        }
        File[] fileList = dir.listFiles ();
        if ( fileList != null && fileList.length > 0 ) {
            for ( File current : fileList ) {
                Download newDownload = new Download ();
                try {
                    newDownload.loadMetaInfo ( current.getPath () );
                    client.networkInit ( newDownload, Client.action_type.RESTORE );
                    Client.downloads.put ( current.getName (), newDownload );
                    //applicationWindow.c.fireGUIEvent ( new GUIEvent ( Client.downloads.get ( current.getName () ) ) );
                } catch ( ConnectException e ) {
                    JOptionPane.showMessageDialog ( this, "Tracker: " + newDownload.trackerIP + " is offline! \n Try to restart client when the tracker will be online.", "Restore torrent " + current.getName () + " error!", JOptionPane.ERROR_MESSAGE );
                    
                    //uploadTableModel.getValueAt ( i, 0 );
                    uploadTableModel.removeRow(uploadTableModel.getRowCount()-1);
                    
                    //current.delete ();

                } catch ( IOException e ) {
                    JOptionPane.showMessageDialog ( this, current.getPath () + " (Impossibile to find torrent: the file has been moved or deleted.)", "Restore download error!", JOptionPane.ERROR_MESSAGE );
                    current.delete ();
                } catch ( Exception e ) {
                    JOptionPane.showMessageDialog ( this, e.toString (), "Restore download error!", JOptionPane.ERROR_MESSAGE );
                    current.delete ();
                }
            }

        }

    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$ ();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$ () {
        contentPane = new JPanel ();
        contentPane.setLayout ( new GridLayoutManager ( 1, 1, new Insets ( 10, 10, 10, 10 ), -1, -1 ) );
        contentPane.setMaximumSize ( new Dimension ( 800, 600 ) );
        contentPane.setPreferredSize ( new Dimension ( 600, 400 ) );
        tabs = new JTabbedPane ();
        contentPane.add ( tabs, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension ( 200, 200 ), null, 0, false ) );
        final JPanel panel1 = new JPanel ();
        panel1.setLayout ( new GridLayoutManager ( 1, 1, new Insets ( 0, 0, 0, 0 ), -1, -1 ) );
        tabs.addTab ( "Downloads", new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/disk_black.png" ) ), panel1 );
        final JScrollPane scrollPane1 = new JScrollPane ();
        panel1.add ( scrollPane1, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, 1, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension ( 968, 418 ), null, 0, false ) );
        tableUploads = new JTable ();
        tableUploads.setAutoCreateRowSorter ( true );
        scrollPane1.setViewportView ( tableUploads );
        final JPanel panel2 = new JPanel ();
        panel2.setLayout ( new GridLayoutManager ( 4, 3, new Insets ( 0, 0, 0, 0 ), -1, -1 ) );
        tabs.addTab ( "Search", new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/binocular.png" ) ), panel2 );
        searchJTextField = new JTextField ();
        searchJTextField.setText ( "" );
        panel2.add ( searchJTextField, new GridConstraints ( 0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        searchButton = new JButton ();
        searchButton.setIcon ( new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/magnifier.png" ) ) );
        searchButton.setText ( "Search" );
        panel2.add ( searchButton, new GridConstraints ( 0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        saveLocationJTextField = new JTextField ();
        saveLocationJTextField.setText ( "" );
        panel2.add ( saveLocationJTextField, new GridConstraints ( 3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        downloadButton = new JButton ();
        downloadButton.setIcon ( new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/disk_black.png" ) ) );
        downloadButton.setText ( "Download" );
        panel2.add ( downloadButton, new GridConstraints ( 3, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final JLabel label1 = new JLabel ();
        label1.setText ( "Search" );
        panel2.add ( label1, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final JLabel label2 = new JLabel ();
        label2.setText ( "Results" );
        panel2.add ( label2, new GridConstraints ( 1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final JLabel label3 = new JLabel ();
        label3.setText ( "Destination" );
        panel2.add ( label3, new GridConstraints ( 3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final Spacer spacer1 = new Spacer ();
        panel2.add ( spacer1, new GridConstraints ( 2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false ) );
        final JScrollPane scrollPane2 = new JScrollPane ();
        panel2.add ( scrollPane2, new GridConstraints ( 1, 1, 2, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false ) );
        searchResultsJList = new JList ();
        scrollPane2.setViewportView ( searchResultsJList );
        final JPanel panel3 = new JPanel ();
        panel3.setLayout ( new GridLayoutManager ( 4, 3, new Insets ( 0, 0, 0, 0 ), -1, -1 ) );
        tabs.addTab ( "Publish", new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/box.png" ) ), panel3 );
        localPathJTextField = new JTextField ();
        localPathJTextField.setText ( "" );
        panel3.add ( localPathJTextField, new GridConstraints ( 0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        final JLabel label4 = new JLabel ();
        label4.setText ( "File" );
        panel3.add ( label4, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        browseButton = new JButton ();
        browseButton.setText ( "Browse" );
        panel3.add ( browseButton, new GridConstraints ( 0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        creekLocationJTextField = new JTextField ();
        creekLocationJTextField.setVisible ( false );
        panel3.add ( creekLocationJTextField, new GridConstraints ( 2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        
        publishButton = new JButton ();
        publishButton.setText ( "Publish" );
        panel3.add ( publishButton, new GridConstraints ( 1, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        
        final Spacer spacer2 = new Spacer ();
        panel3.add ( spacer2, new GridConstraints ( 3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false ) );
        final JPanel panel4 = new JPanel ();
        panel4.setLayout ( new GridLayoutManager ( 1, 1, new Insets ( 0, 0, 0, 0 ), -1, -1 ) );
        tabs.addTab ( "Logs", new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/clipboard_text.png" ) ), panel4 );
        final JScrollPane scrollPane3 = new JScrollPane ();
        scrollPane3.setAutoscrolls ( true );
        panel4.add ( scrollPane3, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false ) );
        logsJList = new JList ();
        logsJList.setAutoscrolls ( true );
        logsJList.setSelectionMode ( 0 );
        scrollPane3.setViewportView ( logsJList );
        final JPanel panel5 = new JPanel ();
        panel5.setLayout ( new GridLayoutManager ( 5, 2, new Insets ( 0, 0, 0, 0 ), -1, -1 ) );
        tabs.addTab ( "Options", new ImageIcon ( getClass ().getResource ( "/BitCreeekClient/res/equalizer.png" ) ), panel5 );
        torrentIPJTextField = new JTextField ();
        try {
        	torrentIPJTextField.setText ( PeerConfig.getServerAddress() );
        } catch ( Exception e ) {
        	torrentIPJTextField.setText ( "127.0.0.1" );
        	PeerConfig.setServerAddress( "127.0.0.1" );
        }
        panel5.add ( torrentIPJTextField, new GridConstraints ( 0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        final JLabel label5 = new JLabel ();
        label5.setText ( "Tracker IP" );
        panel5.add ( label5, new GridConstraints ( 0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final JLabel label6 = new JLabel ();
        label6.setText ( "Torrent Location" );
        panel5.add ( label6, new GridConstraints ( 1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        okButton = new JButton ();
        okButton.setText ( "Save Config" );
        panel5.add ( okButton, new GridConstraints ( 4, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        torrentLocationJTextField = new JTextField ();
        try { 
        	torrentLocationJTextField.setText ( PeerConfig.getTorrentsDirPath() );
        } catch ( Exception e ) {
        	torrentLocationJTextField.setText ( System.getProperty( "user.home" ) + "/.BitCreekConfig/torrents" );
        	
        }
        panel5.add ( torrentLocationJTextField, new GridConstraints ( 1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
        final JLabel label7 = new JLabel ();
        label7.setText ( "Download Location" );
        panel5.add ( label7, new GridConstraints ( 2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false ) );
        final Spacer spacer3 = new Spacer ();
        panel5.add ( spacer3, new GridConstraints ( 3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false ) );
        downloadLocationJTextField = new JTextField ();
        try { 
        	downloadLocationJTextField.setText ( PeerConfig.getDownloadsDirPath() );
        } catch ( Exception e ) {
        	downloadLocationJTextField.setText ( System.getProperty( "user.home" ) + "/.BitCreekConfig/downloads" );
        	
        }
        panel5.add ( downloadLocationJTextField, new GridConstraints ( 2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension ( 150, -1 ), null, 0, false ) );
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$ () {
        return contentPane;
    }

    private class ProgressRenderer implements TableCellRenderer {
        private final JProgressBar b = new JProgressBar ( 0, 100 );

        public ProgressRenderer () {
            super ();
            b.setBorder ( BorderFactory.createEmptyBorder ( 1, 1, 1, 1 ) );
        }

        public Component getTableCellRendererComponent ( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
            int i = Integer.parseInt ( ( String ) value );
            b.setValue ( i );
            b.setStringPainted ( true );
            return b;
        }
    }


}
